package com.hope.photoviewer;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hope.photoviewer.adapter.ImageListAdapter;
import com.hope.photoviewer.preview.ImagePreviewActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    private List<ImageInfo> images = new ArrayList<>();
    private ImageListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        adapter = new ImageListAdapter(R.layout.item_image);
        recyclerView.setAdapter(adapter);
        initData();

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter quickAdapter, View view, int position) {
                List<ImageInfo> images = new ArrayList<>();
                for (ImageInfo p : adapter.getData()) {
                    View v = adapter.getViewByPosition(recyclerView, adapter.getData().indexOf(p), R.id.image);
                    int[] location = new int[2];
                    ImageInfo i = new ImageInfo();
                    if (v != null) {
                        v.getLocationOnScreen(location);
                        i.setImageViewX(location[0]);
                        i.setImageViewY(location[1]);
                        i.setImageViewWidth(v.getWidth());
                        i.setImageViewHeight(v.getHeight());
                    }
                    i.setBigImageUrl(p.getBigImageUrl());
                    i.setThumbnailUrl(p.getThumbnailUrl());
                    images.add(i);
                }

                Intent intent = new Intent(MainActivity.this, ImagePreviewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(ImagePreviewActivity.IMAGE_INFO, (Serializable) images);
                bundle.putInt(ImagePreviewActivity.CURRENT_ITEM, position);
                intent.putExtras(bundle);
                startActivity(intent);
                overridePendingTransition(0, 0);
            }
        });
    }

    private void initData() {
        ImageInfo p1 = new ImageInfo();
        p1.setBigImageUrl("http://f.hiphotos.baidu.com/image/pic/item/faf2b2119313b07e97f760d908d7912396dd8c9c.jpg");
        ImageInfo p2 = new ImageInfo();
        p2.bigImageUrl = "http://g.hiphotos.baidu.com/image/pic/item/4b90f603738da977c76ab6fab451f8198718e39e.jpg";

        ImageInfo p3 = new ImageInfo();
        p3.bigImageUrl = "http://e.hiphotos.baidu.com/image/pic/item/902397dda144ad343de8b756d4a20cf430ad858f.jpg";

        ImageInfo p4 = new ImageInfo();
        p4.bigImageUrl = "http://a.hiphotos.baidu.com/image/pic/item/a6efce1b9d16fdfa0fbc1ebfb68f8c5495ee7b8b.jpg";

        ImageInfo p5 = new ImageInfo();
        p5.bigImageUrl = "http://b.hiphotos.baidu.com/image/pic/item/a71ea8d3fd1f4134e61e0f90211f95cad1c85e36.jpg";

        ImageInfo p6 = new ImageInfo();
        p6.bigImageUrl = "http://c.hiphotos.baidu.com/image/pic/item/7dd98d1001e939011b9c86d07fec54e737d19645.jpg";

        ImageInfo p7 = new ImageInfo();
        p7.bigImageUrl = "http://pica.nipic.com/2007-10-17/20071017111345564_2.jpg";

        ImageInfo p8 = new ImageInfo();
        p8.bigImageUrl = "http://pic4.nipic.com/20091101/3672704_160309066949_2.jpg";

        ImageInfo p9 = new ImageInfo();
        p9.bigImageUrl = "http://pic4.nipic.com/20091203/1295091_123813163959_2.jpg";

        images.add(p1);
        images.add(p2);
        images.add(p3);
        images.add(p4);
        images.add(p5);
        images.add(p6);
        images.add(p7);
        images.add(p8);
        images.add(p9);

        adapter.setNewData(images);
    }
}
